#include "wavedialog.h"
#include "ui_wavedialog.h"

#include "Defs.h"
#include "convolution.h"

#include <QPainter>

WaveDialog::WaveDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WaveDialog)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint);
    setWindowTitle(tr("Wave"));
    setModal(true);
    setFixedSize(372, 239);

    connect(ui->wavelengthSlider, SIGNAL(valueChanged(int)), this, SLOT(emitParametersChangedSignal(int)));
    connect(ui->wavelengthSpinBox, SIGNAL(valueChanged(int)), ui->wavelengthSlider, SLOT(setValue(int)));
    connect(ui->amplitudeSlider, SIGNAL(valueChanged(int)), this, SLOT(emitParametersChangedSignal(int)));
    connect(ui->amplitudeSpinBox, SIGNAL(valueChanged(int)), ui->amplitudeSlider, SLOT(setValue(int)));
    connect(ui->phaseSlider, SIGNAL(valueChanged(int)), this, SLOT(emitParametersChangedSignal(int)));
    connect(ui->phaseSpinBox, SIGNAL(valueChanged(int)), ui->phaseSlider, SLOT(setValue(int)));

    connect(ui->horRadioButton, SIGNAL(clicked()), this, SLOT(radioButtonClicked()));
    connect(ui->verRadioButton, SIGNAL(clicked()), this, SLOT(radioButtonClicked()));
    connect(ui->cirRadioButton, SIGNAL(clicked()), this, SLOT(radioButtonClicked()));

    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked()));
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
}

WaveDialog::~WaveDialog()
{
    delete ui;
}

void WaveDialog::emitParametersChangedSignal(int para)
{
    QWidget* widget = qobject_cast<QWidget*>(sender());
    if (widget)
    {
        if (widget->objectName() == "wavelengthSlider")
        {
            ui->wavelengthSpinBox->setValue(para);
        }
        else if (widget->objectName() == "amplitudeSlider")
        {
            ui->amplitudeSpinBox->setValue(para);
        }
        else if (widget->objectName() == "phaseSlider")
        {
            ui->phaseSpinBox->setValue(para);
        }
        else
        {
            return;
        }
        uint wavelength = ui->wavelengthSlider->value();
        uint amplitude = ui->amplitudeSlider->value();
        int phase = ui->phaseSlider->value();
        double angle = (double)phase * PI / 180.0;
        uint type = HORIZONTAL_WAVE;
        if (ui->verRadioButton->isChecked())
        {
            type = VERTICAL_WAVE;
        }
        else if (ui->cirRadioButton->isChecked())
        {
            type = CIRCLE_WAVE;
        }

        emit parametersChanged(wavelength, amplitude, angle, type);
    }
}


void WaveDialog::applyButtonClicked()
{
    QString str = "";
    uint wavelength = ui->wavelengthSlider->value();
    uint amplitude = ui->amplitudeSlider->value();
    int phase = ui->phaseSlider->value();

    if (ui->verRadioButton->isChecked())
    {
        str = "Vertical Wave Filter. ";
    }
    else if (ui->cirRadioButton->isChecked())
    {
        str = "Circle Wave Filter. ";
    }
    else
    {
        str = "Horizontal Wave Filter. ";
    }

    str += "Wave Length : " + QString::number(wavelength)
            + ", Amplitude : " + QString::number(amplitude)
            + ", Phase(in degree) : " + QString::number(phase) + ".";

    emit applyOperator(str);
    close();
}

void WaveDialog::cancelButtonClicked()
{
    close();
}

void WaveDialog::closeEvent(QCloseEvent *)
{
    emit cancelOperator();
}

void WaveDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::gray);
    painter.drawRect(10, 10, width() - 20, height() - 20);
}

void WaveDialog::showEvent(QShowEvent *)
{
    ui->wavelengthSpinBox->setValue(5);
    ui->amplitudeSpinBox->setValue(5);
    ui->phaseSpinBox->setValue(0);
    ui->horRadioButton->setChecked(true);
}

void WaveDialog::radioButtonClicked()
{
    uint wavelength = ui->wavelengthSlider->value();
    uint amplitude = ui->amplitudeSlider->value();
    int phase = ui->phaseSlider->value();
    double angle = (double)phase * PI / 180.0;

    QWidget* widget = qobject_cast<QWidget*>(sender());
    if (widget)
    {
        if (widget->objectName() == "horRadioButton")
        {
            emit parametersChanged(wavelength, amplitude, angle, HORIZONTAL_WAVE);
        }
        else if (widget->objectName() == "verRadioButton")
        {
            emit parametersChanged(wavelength, amplitude, angle, VERTICAL_WAVE);
        }
        else if (widget->objectName() == "cirRadioButton")
        {
            emit parametersChanged(wavelength, amplitude, angle, CIRCLE_WAVE);
        }
    }
}
